package com.nidu.demo.mail.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 邮件日志领域实体
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class MailLog extends BaseTenantEntity implements BaseDeletableEntity {

    private Long id;

    /**
     * 用户编号
     */
    private Long userId;

    /**
     * 用户类型
     */
    private Integer userType;

    /**
     * 接收邮箱地址
     */
    private List<String> toMails;

    /**
     * 抄送邮箱地址
     */
    private List<String> ccMails;

    /**
     * 密送邮箱地址
     */
    private List<String> bccMails;

    /**
     * 邮箱账号编号
     */
    private Long accountId;

    /**
     * 发送邮箱地址
     */
    private String fromMail;

    /**
     * 模板编号
     */
    private Long templateId;

    /**
     * 模板编码
     */
    private String templateCode;

    /**
     * 模版发送人名称
     */
    private String templateNickname;

    /**
     * 邮件标题
     */
    private String templateTitle;

    /**
     * 邮件内容
     */
    private String templateContent;

    /**
     * 邮件参数
     */
    private Map<String, Object> templateParams;

    /**
     * 发送状态
     */
    private Integer sendStatus;

    /**
     * 发送时间
     */
    private LocalDateTime sendTime;

    /**
     * 发送返回的消息 ID
     */
    private String sendMessageId;

    /**
     * 发送异常
     */
    private String sendException;

    /**
     * 是否删除
     */
    private Boolean deleted;

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.MAIL_LOG_NOT_FOUND.getCode(), ErrorCodeConstants.MAIL_LOG_NOT_FOUND.getMessage());
        }
    }

    public static MailLog create(Long userId, Integer userType, List<String> toMails, List<String> ccMails, List<String> bccMails,
                                 Long accountId, String fromMail, Long templateId, String templateCode, String templateNickname,
                                 String templateTitle, String templateContent, Map<String, Object> templateParams) {
        MailLog mailLog = new MailLog();
        mailLog.setUserId(userId)
                .setUserType(userType)
                .setToMails(toMails)
                .setCcMails(ccMails)
                .setBccMails(bccMails)
                .setAccountId(accountId)
                .setFromMail(fromMail)
                .setTemplateId(templateId)
                .setTemplateCode(templateCode)
                .setTemplateNickname(templateNickname)
                .setTemplateTitle(templateTitle)
                .setTemplateContent(templateContent)
                .setTemplateParams(templateParams)
        ;
        return mailLog;
    }
}
